package yt.kpool.activity.salesman.salesmanfragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.GuestDetailBean;
import yt.kpool.bean.LineChartTestBean;
import yt.kpool.interfac.OnAgentDetailsListener;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.AgentLineChartManager;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TimeUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.Util;

/**
 * 代理全部客户
 * @Author:Created by  ygy
 * @time:2020/2/24
 */
public class AgentAllGuestFragment extends Fragment {

    @BindView(R.id.tv_totalMinerOnNumber)
    TextView tv_totalMinerOnNumber;//矿机数量
    @BindView(R.id.tv_MinerOnNumber)
    TextView tv_MinerOnNumber;//在线矿机数
    @BindView(R.id.tv_MinerOffNumber)
    TextView tv_MinerOffNumber;//离线矿机数
    @BindView(R.id.tv_Hashrate)
    TextView tv_Hashrate;//15分钟算力
    @BindView(R.id.tv_Hashrate24H)
    TextView tv_Hashrate24H;//24小时算力
    @BindView(R.id.tv_unit)
    TextView tv_unit;
    @BindView(R.id.dynamic_chart2)
    LineChart mChart2;//曲线图
    private AgentLineChartManager dynamicLineChartManager2;
    private List<CurrencyList.DataBean> currencyListData;

    private int currencyId;//币种id

    private String userid="";

    private OnAgentDetailsListener onAgentDetailsListener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_agentself, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        userid=onAgentDetailsListener.getAgentUserId();

    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false, priority = 0)
    public void handleData(String str) {

        if (str.equals("AgentDetail")) {
            currencyId=onAgentDetailsListener.getAgentCurrencyId();
             getData();
        }


    }
    // 当FRagmen被加载到activity的时候会被回调
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof OnAgentDetailsListener) {
            onAgentDetailsListener = (OnAgentDetailsListener) activity; // 2.2 获取到宿主activity并赋值
        } else {
            throw new IllegalArgumentException("activity must implements FragmentInteraction");
        }
    }

    /**
     * 获取客户详情数据
     */
    private void getData() {
        if (NetWorkUtil.check(getActivity())) {


            NetHelperNew.AgentDetails(currencyId + "",userid, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {

                    Log.i("-->", "代理全部客户详情：" + data);
                    GuestDetailBean guestDetailBean = MyGson.getInstance().fromJson(data, GuestDetailBean.class);
                    if (guestDetailBean.getType() == 1) {
                        GuestDetailBean.DataBean data1 = guestDetailBean.getData();


                        tv_totalMinerOnNumber.setText(data1.getMinerOnNumber() + data1.getMinerOffNumber() + "台");//矿机数量

                        tv_MinerOnNumber.setText(data1.getMinerOnNumber() + "台");//在线矿机数

                        tv_MinerOffNumber.setText(data1.getMinerOffNumber() + "台");//离线矿机数

                        tv_Hashrate.setText(Util.FormatHashrate(data1.getHashrate()) + "");//15分钟算力

                        tv_Hashrate24H.setText(Util.FormatHashrate(data1.getHashrate24H()) + "");//24小时算力


                        List<GuestDetailBean.DataBean.HashrateChartBean> dataBeans = data1.getHashrateChart();
                        List<LineChartTestBean> list = new ArrayList<>();

                        String unit = "";
                        float max = 0;
                        for (int i = 0; i < dataBeans.size(); i++) {
                            if (i == 0) {
                                unit = dataBeans.get(i).getHashrateUnit();
                            }
                            try {
                                list.add(new LineChartTestBean(TimeUtil.dealDateFormatNomm(dataBeans.get(i).getCreatedTime()), (float) dataBeans.get(i).getHashrate()));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            if (dataBeans.get(i).getHashrate() > max) {
                                max = (float) dataBeans.get(i).getHashrate();
                            }
                        }


                        List<String> names = new ArrayList<>(); //折线名字集合
                        List<Integer> colour = new ArrayList<>();//折线颜色集合
                        //折线名字 //折线颜色
                        names.add("矿工");
                        colour.add(getResources().getColor(R.color.black3));


                        tv_unit.setText("" + unit);
                        dynamicLineChartManager2 = new AgentLineChartManager(mChart2, names, colour, 48, unit);
                        dynamicLineChartManager2.setDescription("");
                        dynamicLineChartManager2.setYAxis(max, 0, 6);
                        for (int i = 0; i < list.size(); i++) {
                            dynamicLineChartManager2.addEntry(list.get(i));
                        }
                    } else {
                        ToastUtil.showToast(getActivity(), guestDetailBean.getContent());

                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);


                    ToastUtil.showToast(getActivity(), msg);
                }
            });


        }
    }

}
